package com.yanqu.road.server.cmd.activity.jiuzhou.cross;

import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouConfig;
import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouSkillConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.jiuzhou.CrossJiuZhouProto;
import com.yanqu.road.pb.activity.jiuzhou.JiuZhouProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.jiuzhou.JiuZhouMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = GameProtocol.S_JIU_ZHOU_CHOOSE_SKILLID2_FROM_CROSS, desc = "选择棋书")
public class JiuZhouChooseSkillId2CmdFromCross extends AbstractCommand {


    private int oldSkillId2;

    private int newSkillId2;

    private JiuZhouProto.JiuZhouDeployDataMsg deployTemp;


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossJiuZhouProto.CrossJiuZhouChooseSkillId2RespMsg reqMsg = CrossJiuZhouProto.CrossJiuZhouChooseSkillId2RespMsg.parseFrom(packet.getBytes());
        ret = reqMsg.getRet();
        if (reqMsg.hasOldSkillId2()) {
            oldSkillId2 = reqMsg.getOldSkillId2();
        }
        if (reqMsg.hasNewSkillId2()) {
            newSkillId2 = reqMsg.getNewSkillId2();
        }
        if (reqMsg.hasDeploy()) {
            deployTemp = reqMsg.getDeploy();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        JiuZhouConfig config = JiuZhouMgr.getConfig();
        if (config == null) {
            ret = GameErrorCode.E_JIU_ZHOU_CONFIG_NOT_EXIST;
            return;
        }
        if (newSkillId2 != 0) {
            JiuZhouSkillConfig newSkillId2Config = config.getSkillConfigMap().get(newSkillId2);
            if (newSkillId2Config == null) {
                ret = GameErrorCode.E_JIU_ZHOU_CONFIG_NOT_EXIST;
                return;
            }
            Property cost = new Property(newSkillId2Config.getItemID(), 1);
            // 取消预扣除
            player.getModule(PreReduceModule.class).restorePreReduce(cost);
            if (ret == 0) {
                // 扣除道具
                player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.JiuZhou, eLogMoneyType.JiuZhouChooseSkillId2Cost);
            }
        }

        // 需要返还棋书道具
        if (oldSkillId2 != 0) {
            JiuZhouSkillConfig oldSkillId2Config = config.getSkillConfig(oldSkillId2);
            if (oldSkillId2Config != null) {
                player.getModule(CurrencyModule.class).addCurrency(new Property(oldSkillId2Config.getItemID(), 1), eLogMoneyType.JiuZhou, eLogMoneyType.JiuZhouChooseSkillId2Get);
            }
        }

        if (ret == 0) {
            JiuZhouProto.JiuZhouChooseSkillId2RespMsg.Builder respMsg = JiuZhouProto.JiuZhouChooseSkillId2RespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setDeploy(deployTemp);
            player.sendPacket(ClientProtocol.U_JIU_ZHOU_CHOOSE_SKILLID2, respMsg);
        }

    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            JiuZhouProto.JiuZhouChooseSkillId2RespMsg.Builder respMsg = JiuZhouProto.JiuZhouChooseSkillId2RespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_JIU_ZHOU_CHOOSE_SKILLID2, respMsg);
        }
    }
}
